import 'whatwg-fetch'
import Toast from 'components/toast'

export async function post (url, body = {}) {
  return new Promise((resolve, reject) => {
    fetch (url, {
      mode: 'cors',
      method: 'POST',
      headers: {
        'Access-Control-Allow-Origin': '*',
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(body)
    })
      .then((res) => res.json())
      .then((resData) => {
        resolve(resData)
      })
      .catch((err) => {
        Toast.loading(2)
        resolve(err)
      })
  })
}

export async function get (url, body = {}) {
  return new Promise((resolve, reject) => {
    fetch (url, {
      method: 'get',
      mode: 'cors',
      headers: {
        'Access-Control-Allow-Origin': '*',
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
        'Accept': 'application/json,  text/plain, */*'
      }
    })
      .then((res) => res.json())
      .then((resData) => {
        resolve(resData)
      })
      .catch((err) => {
        Toast.loading(2)
        resolve(err)
      })
  })
}
